package net.caixiaomi.info.ui.discovery;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.animation.Animation;
import android.view.animation.Transformation;
import android.widget.HorizontalScrollView;

import net.caixiaomi.info.R;
import net.caixiaomi.info.base.BaseActivity;
import net.caixiaomi.info.interfaces.MatchingSelectCallBack;
import net.caixiaomi.info.ui.view.HornorSelectView;
import net.caixiaomi.info.widgets.DefLoading;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;

/**
 * Created by mac on 2018/9/7.
 */

public class LeaguesHomeActivity extends BaseActivity implements ViewPager.OnPageChangeListener,MatchingSelectCallBack {

    @BindView(R.id.select_view)
    HornorSelectView mSelectView;

    private MyFragmentPagerAdapter adapter;
    private List<String> titles;
    private Context mContext;
    public int mType=0;

    @BindView(R.id.scroll_view)
    HorizontalScrollView scroll_view;

    @BindView(R.id.fragment_view_pager)
    ViewPager fragment_view_pager;

    private ArrayList<Fragment> mFragmentList;

    private HashMap<String,Integer> map=new HashMap<>();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext=this;
        titles=new ArrayList<>();
        titles.add("热门");titles.add("国际");titles.add("欧洲");titles.add("亚洲");titles.add("美洲");titles.add("非洲");
        map.put("热门",0);map.put("国际",4);map.put("欧洲",1);map.put("亚洲",3);map.put("非洲",5);map.put("美洲",2);
        mSelectView.setCallBack(this,titles);

        mLoading = new DefLoading(mContext);

        //view pager
        adapter = new MyFragmentPagerAdapter(getSupportFragmentManager(),
                getFragmentList()
        );
        fragment_view_pager.setAdapter(adapter);
        fragment_view_pager.addOnPageChangeListener(this);
        fragment_view_pager.setOffscreenPageLimit(6);
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        mType=position;
        mSelectView.setPageSelected(position);

        smoothScrollTo(position * mSelectView.subTitleWidth);


        if(position==0){
            LeagueHotFragment ff= (LeagueHotFragment) mFragmentList.get(0);
            ff.initData(map.get(titles.get(0)));
        }else{
            LeaguesHomeFragment fz=(LeaguesHomeFragment) mFragmentList.get(position);
            fz.initData();
        }

//        LeagueHotFragment ff= (LeagueHotFragment) mFragmentList.get(0);
//        ff.setGroupId(map.get(titles.get(0)));
//        for(int i=1;i<titles.size();i++){
//            LeaguesHomeFragment fz=(LeaguesHomeFragment) mFragmentList.get(i);
//            fz.setGroupId(map.get(titles.get(i)),titles.get(i));
//        }
    }

    /**
     * 流畅滑动
     */
    private void smoothScrollTo(final int scrollTo) {
        final int currentPosition = scroll_view.getScrollX();
        Animation anim = new Animation() {
            @Override
            protected void applyTransformation(float interpolatedTime, Transformation t) {
                int scrollOffset = scrollTo - currentPosition;
                scroll_view.scrollTo(currentPosition + (int) (scrollOffset * interpolatedTime), 0);
            }
        };
        anim.setDuration(300);
        scroll_view.startAnimation(anim);
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    @Override
    public void matchingBarSelected(int position) {
        if(mType==position){
//            loadData(mType,true);
        }
        fragment_view_pager.setCurrentItem(position);
    }

    @Override
    protected int layout() {
        return R.layout.hornor_pager_layout;
    }

    private class MyFragmentPagerAdapter extends FragmentPagerAdapter {
        private ArrayList<Fragment> list;

        public MyFragmentPagerAdapter(FragmentManager fm, ArrayList<Fragment> list) {
            super(fm);
            this.list = list;
        }

        @Override
        public Fragment getItem(int arg0) {
            return list.get(arg0);
        }

        @Override
        public int getCount() {
            return list.size();
        }

    }

    /**
     * 生成碎片列表
     */
    private ArrayList<Fragment> getFragmentList() {
        mFragmentList = new ArrayList<>();

        LeagueHotFragment ff=new LeagueHotFragment();
        ff.setGroupId(map.get(titles.get(0)));
        mFragmentList.add(ff);
        for(int i=1;i<titles.size();i++){
            LeaguesHomeFragment fz=new LeaguesHomeFragment();
            fz.setGroupId(map.get(titles.get(i)),titles.get(i));
            mFragmentList.add(fz);
        }
        return mFragmentList;
    }
}
